package com.victor.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.Advertisement;
import com.victor.service.AdvertisementService;
import com.victor.utils.Msg;
import com.victor.utils.OSSUtil;

@RestController
@RequestMapping("/advertisement")
public class AdvertisementController {
	@Autowired
	private AdvertisementService advertisementService;
	@Autowired
	private LogController logcontroller;

	/**
	 * 添加广告
	 * 
	 * @param advertisement
	 * @return
	 */
	@RequestMapping("/add")
	public Msg addAdvertisement(Advertisement advertisement,String adminid) {
		if (advertisement == null || advertisement.getPhoto().isEmpty() || adminid == null  || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean b = advertisementService.insert(advertisement);
		logcontroller.addLog(adminid, "添加了广告");
		return Msg.judge(b);

	}

	/**
	 * 修改广告
	 * 
	 * @param advertisement
	 * @return
	 */
	@RequestMapping("/update")
	public Msg update(Advertisement advertisement,String adminid) {
		if (advertisement == null || advertisement.getId() == null || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean b = advertisementService.update(advertisement);
		logcontroller.addLog(adminid, "修改了广告");
		return Msg.judge(b);

	}

	/**
	 * 广告列表
	 * 
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/getall")
	public Msg getAll(int page, int row,String adminid) {
		if (adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		PageInfo<Advertisement> pageInfo = advertisementService.getAll(page, row);
		logcontroller.addLog(adminid, "查看了广告列表");
		return Msg.success().add("pageInfo", pageInfo);

	}

	/**
	 * 广告详情
	 * 
	 * @param advertisement
	 * @return
	 */
	@RequestMapping("/xiangqing")
	public Msg getAdvertisementById(Advertisement advertisement,String adminid) {
		if (advertisement.getId() == null || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		advertisement = advertisementService.selById(advertisement);
		logcontroller.addLog(adminid, "查看了"+advertisement.getId()+"详情");
		return Msg.success().add("advertisement", advertisement);

	}
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public Msg delete(int[] ids,String adminid) {
		if (ids == null || ids.length <= 0 || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		for (int i = 0; i < ids.length; i++) {
			 Advertisement selById = advertisementService.selById(new Advertisement(ids[i], null, null, null, null));
			 OSSUtil.deleteFile(selById.getPhoto());
		}
		boolean b =advertisementService.delete(ids);
		logcontroller.addLog(adminid, "删除了广告"+ids);
		return Msg.judge(b);

	}

}
